package com.macro.mall.tiny.common.api;

/**
 * 返回通用的对象
 * T表示未知的类型
 */
public class CommonResult<T> {
    private long code;
    private String message;
    private T data;

    public CommonResult(){

    }

    public CommonResult(long code, String message, T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    //成功的情况下返回的结果
    public static <T>CommonResult<T> success(T data){
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }

    public static <T>CommonResult<T> success(T data, String message){
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(),message,data);
    }

    //失败的情况下要返回的结果
    public static <T>CommonResult<T> failed(IErrorCode errorCode){
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
    }

    public static <T>CommonResult<T> failed(String message){
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
